﻿using FighttheLandOwner.GameCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Server
{
    class CardConvert
    {
        /// <summary>
        /// 将牌堆转化为string，“%”分开不同的牌，“&”分开一张牌的各种类型
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        public static string ToString(Card card)
        {
            return Card.GetAttributeValue(card.Color) + "&" + Card.GetAttributeValue(card.Type) + "&" + Card.GetAttributeValue(card.Value);
        }

        /// <summary>
        /// 将牌堆转化为string，“%”分开不同的牌，“&”分开一张牌的各种类型
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        public static string ToString(List<Card> cards)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(ToString(cards[0]));
            int i = 0;
            foreach(var c in cards)
            {
                if (i == 0) { i++; continue; }
                builder.Append("%" + ToString(c));
            }
            return builder.ToString();
        }

        public static Card ToCard(string card)
        {
            var vs = card.Split('&');
            var color = Card.GetCardColor(vs[0]);
            var type = Card.GetCardType(vs[1]);
            var value = Card.GetCardValue(vs[2]);
            return new Card(type, value);
        }

        public static List<Card> ToCards(string cards)
        {
            var vs = cards.Split('%');
            List<Card> result = new List<Card>();
            foreach (var p in vs)
            {
                result.Add(ToCard(p));
            }
            return result;
        }
    }
}
